package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class ReverseSubstringsBetweenEachPairOfParentheses {
    // 2022-08-19 15:17:59
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String reverseParentheses(String s) {
            StringBuilder ans = new StringBuilder();
            char[] chars = s.toCharArray();
            Stack<Character> stack = new Stack<>();
            Queue<Character> temp = new LinkedList<>();
            for (char aChar : chars) {
                if (aChar == ')') {
                    while (stack.peek() != '(') {
                        Character pop = stack.pop();
                        temp.offer(pop);
                    }
                    stack.pop();
                    while (!temp.isEmpty()) {
                        stack.push(temp.poll());
                    }
                } else {
                    stack.push(aChar);
                }
            }
            stack.forEach(ans::append);
            return ans.toString();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}